﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponentsDx.Browsers;
using ProgNetComponentsDx.Language;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;

namespace ProgNetComponentsDx.Forms
{
    public partial class FormMultiSelect : XtraForm
    {
        private FormMultiSelect()
        {
            InitializeComponent();
        }
        Browser _Browser;

        public FormMultiSelect(string browser, int width)
            : this()
        {
            _Browser = Browsers.BrowserCache.GetBrowser(browser);
            this.Width = width;
            if (_Browser != null)
            {
                DataView DataView = new DataView(_Browser.GetTable());
                DataView.RowFilter = _Browser.Filter;

                if (_Browser.Caption.IsNullOrEmpty())
                    this.Text = ProgNetLanguage.sGetStringDefault(_Browser.Name, _Browser.Name);
                else
                    this.Text = _Browser.Caption;
                GridColumn col;
                modeProvider1.BeginInit();
                foreach (BrowserColumn c in _Browser.Columns)
                {
                    col = BrowserTools.CreateColumn(c, gvMain, this.browserProvider1);
                    col.OptionsColumn.ReadOnly = true;
                    gvMain.Columns.Add(col);
                    modeProvider1.GetSettings(col).ReadOnlyModes = Enums.FormMode.All;
                }
                modeProvider1.EndInit();
                modeProvider1.CurrentMode = Enums.FormMode.Edit;
                colMark.Width = 15;
                colMark.Fixed = FixedStyle.Left;
                gcMain.DataSource = DataView;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        public DataRow[] SelectedRows
        {
            get { return rows.ToArray(); }
        }

        List<DataRow> rows = new List<DataRow>();

        private void gvMain_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            DataRow row = ((DataView)gcMain.DataSource)[e.ListSourceRowIndex].Row;
            if (row != null)
            {
                if (e.IsGetData)
                {
                    e.Value = rows.Contains(row);
                }
                if(e.IsSetData)
                {
                    if (e.Value.AsBool())
                        rows.Add(row);
                    else
                        rows.Remove(row);
                }
            }
        }
    }
}
